package com.baoyouqun.controller.back;

import cn.dev33.satoken.stp.StpUtil;
import com.alibaba.cola.dto.SingleResponse;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baoyouqun.base.PageResponse;
import com.baoyouqun.domain.CreateCmd.UserSearchRecordCreateCmd;
import com.baoyouqun.domain.Query.UserSearchRecordPageQuery;
import com.baoyouqun.domain.Query.UserSearchRecordQuery;
import com.baoyouqun.domain.VO.KeywordFrequencyVO;
import com.baoyouqun.domain.VO.UserSearchRecordVO;
import com.baoyouqun.service.UserSearchRecordService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/userSearchRecord")
@Api(tags = "用户搜索记录表管理接口")
public class UserSearchRecordController {

    @Resource
    private UserSearchRecordService userSearchRecordService;

    @PostMapping("create")
    @ApiOperation("新增用户搜索记录表")
    public SingleResponse<UserSearchRecordVO> create(@RequestBody UserSearchRecordCreateCmd cmd) {

        return userSearchRecordService.create(cmd);
    }

    @GetMapping("page")
    @ApiOperation("分页查询用户搜索记录表")
    public PageResponse<UserSearchRecordVO> page(UserSearchRecordQuery query) {
        String userId = StpUtil.getLoginIdAsString();
        query.setUserId(userId);
        IPage<UserSearchRecordVO> page = userSearchRecordService.pageVo(query);
        return PageResponse.of(page.getRecords(), page.getTotal(), page.getCurrent(), page.getSize());
    }

    @GetMapping("highFrequencyKeywords")
    @ApiOperation("查询高频词云（所有用户关键词及频次）")
    public PageResponse<KeywordFrequencyVO> getHighFrequencyKeywords(Long pageSize,
                                                                     Long current,
                                                                     String keyword,
                                                                     Boolean isAsc) {
        // 仅管理员可访问
//        StpUtil.checkRole("admin");
        IPage<KeywordFrequencyVO> page = userSearchRecordService.getHighFrequencyKeywords(pageSize, current, keyword, isAsc);
        return PageResponse.of(page.getRecords(), page.getTotal(), page.getCurrent(), page.getSize());

    }

    /**
     * 按条件查询关键词关联的用户记录（分页）
     * 仅管理员可访问
     */
    @GetMapping("pageUserByKeyword")
    @ApiOperation("按条件查询用户搜索记录（分页）")
    public PageResponse<UserSearchRecordVO> pageUserByKeyword(UserSearchRecordPageQuery query) {
        SingleResponse<IPage<UserSearchRecordVO>> response = userSearchRecordService.pageUserByKeyword(query);
        if (!response.isSuccess()) {
            return new PageResponse<>();
        }

        IPage<UserSearchRecordVO> page = response.getData();
        return PageResponse.of(page.getRecords(), page.getTotal(), page.getCurrent(), page.getSize()      // 每页条数
        );
    }

    /**
     * 导出用户搜索记录Excel（按当前筛选条件）
     * 仅管理员可访问
     */
    @GetMapping("exportExcel")
    @ApiOperation("导出用户搜索记录Excel")
    public void exportExcel(UserSearchRecordPageQuery query, HttpServletResponse response) {
        // 直接调用Service导出逻辑（Service已处理响应流）
        userSearchRecordService.exportUserSearchRecords(query, response);
    }
}